﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class InfoController : AdminBaseController
    {
        infoCore core = new infoCore();
        public ActionResult Get(int id)
        {
            if (!HasQx("system"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetOne(id));
        }
        public ActionResult GetList(pageinfo pageinfo)
        {
            if (!HasQx("system"))
            {
                return Error("没有权限！");
            }
            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and (name like @" + argsList.Count + " or weburl  like @" + argsList.Count + ")";
                argsList.Add($"%{pageinfo.key}%");
            }
            var list = core.GetPageList(pageinfo.page,pageinfo.pagesize, where, pageinfo.order+" "+pageinfo.esc, argsList.ToArray());
            return Success(list);
        }
        
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("system"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username + "删除站点ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<infoEntity> modelList)
        {
            if (!HasQx("system"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var infoEntity in modelList)
            {
                core.Del(infoEntity);
                ids += infoEntity.ID + ",";
            }
            ids = ids.TrimEnd(',');
            Log(curmanage.username + "删除站点ID in (" + ids + ")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult Save(infoEntity info)
        {
            if (!HasQx("system"))
            {
                return Error("没有权限！");
            }
            if (info.name == null)
            {
                return Error();
            }
            var model = core.Save(info);
            Log(curmanage.username + "保存站点" + model.ID);
            return Success(model);
        }
    }
}
